<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Repository;

use Plugin\Alarm\Model\RdAlarmBroadcastRecord;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 广播记录 Repository类
 */
class RdAlarmBroadcastRecordRepository extends IRepository
{
   public function __construct(
        protected readonly RdAlarmBroadcastRecord $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 文件id
        if (isset($params['file_id']) && filled($params['file_id'])) {
            $query->where('file_id', '=', $params['file_id']);
        }

        // 秒
        if (isset($params['second']) && filled($params['second'])) {
            $query->where('second', '=', $params['second']);
        }

        // 广播id
        if (isset($params['device']) && filled($params['device'])) {
            $query->where('device', '=', $params['device']);
        }

        // 类型 1:广播 2:喊话
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 开始时间
        if (isset($params['start_time']) && filled($params['start_time'])) {
            $query->where('start_time', '=', $params['start_time']);
        }

        // 结束时间
        if (isset($params['end_time']) && filled($params['end_time'])) {
            $query->where('end_time', '=', $params['end_time']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 创建时间范围查询
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间范围查询
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}